# -*- coding: utf8 -*-
import os
import sys
import datetime
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from book.models import *
from book import utils
import logging

class MainPage(webapp.RequestHandler):
    def get(self):
        shops = Shop.all()
        context = {
                   'shops' : shops,
                   'current_module' : 'booking'
                }

        utils.render_to_response(self.response, "booking.html", context)
        
    def post(self):
        emp_code = self.request.get('emp_code')
        emp_name = self.request.get('emp_name')
        shop_key = self.request.get('shop_key')
        if emp_code and emp_name and shop_key:
            shop = Shop.get(db.Key(shop_key))
            if shop:
                dish_keys = self.request.get_all('dish_keys')
                for dish_key in dish_keys:
                    dish = Dish.get(db.Key(dish_key))
                    if dish:
                        user = User.gql('WHERE code = :1', emp_code).get()
                        if not user:
                            user = User(code = emp_code, name = emp_name)
                            user.put()
                    
                        order = Order(user = user, dish = dish, date = datetime.date.today())
                        order.put()                   
                        balances = Balance.gql('WHERE user = :1 and date = :2', user,datetime.date.today()).fetch(1)
                        if not balances:    
                            balance = Balance(user = user, realPay = 0.0, balancePay = dish.price, date = datetime.date.today())
                            balance.put()            
                        else:
                            b = balances[0]
                            b.balancePay = b.balancePay + dish.price    
                            b.put()   
                        self.redirect('/mybooking/%s' % emp_code)

        shops = Shop.all()
        context = {
           'shops' : shops,
           'current_module' : 'booking'
        }
        utils.render_to_response(self.response, "booking.html", context)
        
class DishPage(webapp.RequestHandler):   
    def get(self, shop_key):
        shop = Shop.get(db.Key(shop_key))
        if shop:
            q = db.Query(Dish)
            dishes = q.filter('shop = ', shop).order('price').fetch(200)
        else:
            dishes = []
            
        context = {
            'dishes' : dishes
        }
        
        utils.render_to_response(self.response,"bookingdish.html", context)  


class UserPage(webapp.RequestHandler):        
    def get(self, emp_id):
        user = User.gql('WHERE code = :1', emp_id).get()
        if user:
            context = {
                 'user' : user
            }
            utils.render_to_response(self.response, "user.xml", context)
        else:
            utils.render_to_error(self.response, 'user_e001', u'用户不存在')


